<?php
$prefix = 'wpbl_';

$meta_box = array(
	'id' => 'my-meta-box',
	'title' => 'Aviablity',
	'page' => 'library',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array(
			'name' => 'Aviablity',
			'id' => $prefix . 'aviablity',
			'type' => 'radio',
			'options' => array(
				array('name' => 'Aviable', 'value' => 'Aviable'),
				array('name' => 'Rented', 'value' => 'Rented')
			)
		)
	)
);

add_action('admin_menu', 'aviablity_add_box');

// Add meta box
function aviablity_add_box() {
	global $meta_box;
	
	add_meta_box($meta_box['id'], $meta_box['title'], 'aviablity_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}


// Callback function to show fields in meta box
function aviablity_show_box() {
	global $meta_box, $post;

	// Use nonce for verification
	echo '<input type="hidden" name="aviablity_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
	
	echo '<table class="form-table">';

	foreach ($meta_box['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);

	  if ('' == $meta){
	  $meta = 'Aviable';
	  $_POST[$field['id']] = $meta;
	  update_post_meta($post->ID, 'wpbl_aviablity', $meta);
	  }
		echo '<tr>',
				'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
				'<td>';
		switch ($field['type']) {
			case 'radio':
				foreach ($field['options'] as $option) {
					echo '   <input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />   ', $option['name'];
				}
				break;
		}
		echo 	'<td>',
			'</tr>';
	}	
	echo '</table>';
}


add_action('save_post', 'aviablity_save_data');

// Save data from meta box
function aviablity_save_data($post_id) {
	global $meta_box;
	
	// verify nonce
	if (!wp_verify_nonce($_POST['aviablity_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}

	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}

	// check permissions
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}

	foreach ($meta_box['fields'] as $field) {
		$old = get_post_meta($post_id, $field['id'], true);
		$new = $_POST[$field['id']];
		
		if ($new && $new != $old) {
			update_post_meta($post_id, $field['id'], $new);
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $field['id'], $old);
		}
	}
}

// Add to our admin_init function
add_filter('manage_edit-library_columns', 'aviablity_add_post_columns');
 
function aviablity_add_post_columns($columns) {
	$columns['book_set'] = 'Aviablity';
	return $columns;
}
// Add to our admin_init function
add_action('manage_posts_custom_column', 'aviablity_render_post_columns', 10, 2);
 
function aviablity_render_post_columns($column_name, $id) {
	global $meta_box, $post;
	switch ($column_name) {
	case 'book_set':
		// show book set
      $custom = get_post_custom();
      echo $custom["wpbl_aviablity"][0];			
		break;
	}
}
// Add to our admin_init function
add_action('quick_edit_custom_box',  'aviablity_add_quick_edit', 10, 2);
 
function aviablity_add_quick_edit($column_name, $post_type) {
	
	global $meta_box, $post;
	if ($column_name != 'book_set') return;
	?>
    <fieldset class="inline-edit-col-left">
	<div class="inline-edit-col">
		<?php

	
	// Use nonce for verification
	echo '<input type="hidden" name="aviablity_meta_box_nonce" id="aviablity_meta_box_nonce"  value="', wp_create_nonce(basename(__FILE__)), '" />';
	
	echo '<table class="form-table">';

	foreach ($meta_box['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);
	if ('' == $meta){
	  $meta = 'Aviable';
	  $_POST[$field['id']] = $meta;
	  update_post_meta($post->ID, 'wpbl_aviablity', $meta);
	  }
		echo '<tr>',
				'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
				'<td>';
		switch ($field['type']) {
			case 'radio':
				foreach ($field['options'] as $option) {
					echo '   <input type="radio" name="', $field['id'], '" id="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />   ', $option['name'];
				}
				break;
		}
		echo 	'<td>',
			'</tr>';
	}	
	echo '</table>';
?>
	</div>
    </fieldset>
	<?php
}
// Add to our admin_init function
add_action('save_post', 'aviablity_save_quick_edit_data');
 
function aviablity_save_quick_edit_data($post_id) {
	// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
	// to do anything
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
		return $post_id;	
	// Check permissions
	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ) )
			return $post_id;
	} else {
		if ( !current_user_can( 'edit_post', $post_id ) )
		return $post_id;
	}	
	// OK, we're authenticated: we need to find and save the data
	$post = get_post($post_id);
	if (isset($_POST['wpbl_aviablity']) && ($post->post_type != 'revision')) {
		$book_set_id = esc_attr($_POST['wpbl_aviablity']);
		if ($book_set_id)
			update_post_meta( $post_id, 'wpbl_aviablity', $book_set_id);		
		else
			delete_post_meta( $post_id, 'wpbl_aviablity');		
	}		
	return $book_set_id;	
}
// Add to our admin_init function
add_action('admin_footer', 'aviablity_quick_edit_javascript');
 
function aviablity_quick_edit_javascript() {
	global $current_screen;
	if (($current_screen->id != 'edit-post') || ($current_screen->post_type != 'library')) return; 
 
	?>
	<script type="text/javascript">
	<!--
	function set_inline_book_set(bookSet, nonce) {
		// revert Quick Edit menu so that it refreshes properly
		inlineEditPost.revert();
		var bookInput = document.getElementById('wpbl_aviablity');
		var nonceInput = document.getElementById('aviablity_meta_box_nonce');
		nonceInput.value = nonce;
		// check option manually
		for (i = 0; i < bookInput.options.length; i++) {
			if (bookInput.options[i].value == bookSet) { 
				bookInput.options[i].setAttribute("selected", "selected"); 
			} else { bookInput.options[i].removeAttribute("selected"); }
		}
	}
	//-->
	</script>
	<?php
}
// Add to our admin_init function
add_filter('post_row_actions', 'aviablity_expand_quick_edit_link', 10, 2);
 
function aviablity_expand_quick_edit_link($actions, $post) {
	global $current_screen;
	if (($current_screen->id != 'edit-post') || ($current_screen->post_type != 'library')) return $actions; 
 
	$nonce = wp_create_nonce( 'aviablity_book_set'.$post->ID);
	$book_id = get_post_meta( $post->ID, 'wpbl_aviablity', TRUE);	
	$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="';
	$actions['inline hide-if-no-js'] .= esc_attr( __( 'Edit this item inline' ) ) . '" ';
	$actions['inline hide-if-no-js'] .= " onclick=\"set_inline_book_set('{$book_id}', '{$nonce}')\">"; 
	$actions['inline hide-if-no-js'] .= __( 'Quick&nbsp;Edit' );
	$actions['inline hide-if-no-js'] .= '</a>';
	return $actions;	
}
?>